
Page({
  data: {
    totalTasks: 5,
    completedTasks: 0,
    allTasksCompleted: false,
    progress: 0, // 完成进度百分比
    buttonPositionX: 500, // 默认的X位置
    buttonPositionY: 1000, // 默认的Y位置
    isDragging: false, // 标记是否正在拖动

    uncompletedTasks: [
      { content: "慢跑5公里", checked: false, pinned: false },
      { content: "喝八杯水", checked: false, pinned: false },
      { content: "吃水果", checked: false, pinned: false }
    ],
    completedTasksList: [
      { content: "早起拉伸", checked: true, pinned: false },
      { content: "散步", checked: true, pinned: false },
    ],
    showUncompleted: true,
    showCompleted: true,

    showInput: false,
    newTaskContent: "",
    editingIndex: null,
  },

  onLoad() {
    this.updateProgress();
  },

  updateProgress() {
    const completedTasks = this.data.completedTasksList.length;
    const totalTasks = this.data.uncompletedTasks.length + completedTasks;
    const progress = totalTasks ? Math.round((completedTasks / totalTasks) * 100) : 0;
    this.setData({
      completedTasks,
      totalTasks,
      progress,
      allTasksCompleted: completedTasks === totalTasks,
    });
  },

  toggleUncompleted() {
    this.setData({ showUncompleted: !this.data.showUncompleted });
  },

  toggleCompleted() {
    this.setData({ showCompleted: !this.data.showCompleted });
  },

  markTaskComplete(e) {
    const index = e.currentTarget.dataset.index;
    const task = this.data.uncompletedTasks[index];
    task.checked = true;
    const completedTasksList = [...this.data.completedTasksList, task];
    const uncompletedTasks = this.data.uncompletedTasks.filter((_, i) => i !== index);
    this.setData({ completedTasksList, uncompletedTasks }, this.updateProgress);
  },

  unmarkTaskComplete(e) {
    const index = e.currentTarget.dataset.index;
    const task = this.data.completedTasksList[index];
    task.checked = false;
    const uncompletedTasks = [...this.data.uncompletedTasks, task];
    const completedTasksList = this.data.completedTasksList.filter((_, i) => i !== index);
    this.setData({ uncompletedTasks, completedTasksList }, this.updateProgress);
  },

  showAddTask() {
    this.setData({ showInput: true });
  },
  
  
 
  inputTask(e) {
    this.setData({ newTaskContent: e.detail.value });
  },

  addTask() {
    const { newTaskContent, editingIndex } = this.data;
    if (!newTaskContent.trim()) return;
    if (editingIndex !== null) {
      const uncompletedTasks = [...this.data.uncompletedTasks];
      uncompletedTasks[editingIndex].content = newTaskContent;
      this.setData({ uncompletedTasks, newTaskContent: "", showInput: false, editingIndex: null });
    } else {
      const newTask = { content: newTaskContent, checked: false, pinned: false };
      const uncompletedTasks = [...this.data.uncompletedTasks, newTask];
      this.setData({ uncompletedTasks, newTaskContent: "", showInput: false });
    }

    this.updateProgress();
  },

  cancelAddTask() {
    this.setData({ newTaskContent: "", showInput: false, editingIndex: null });
  },

  editTask(e) {
    const index = e.currentTarget.dataset.index;
    const task = this.data.uncompletedTasks[index];
    this.setData({
      showInput: true,
      newTaskContent: task.content,
      editingIndex: index,
    });
  },

  pinTask(e) {
    const index = e.currentTarget.dataset.index;
    const task = this.data.uncompletedTasks[index];
    task.pinned = !task.pinned;
    const uncompletedTasks = [...this.data.uncompletedTasks];
    uncompletedTasks.splice(index, 1);
    uncompletedTasks.unshift(task);
    this.setData({ uncompletedTasks });
  },

  deleteTask(e) {
    const { index, type } = e.currentTarget.dataset;
    const that = this; // 保存当前上下文

    // 弹出确认框
    wx.showModal({
        title: '确认删除',
        content: '您确定要删除此任务吗？',
        success(res) {
            if (res.confirm) {
                if (type === "uncompleted") {
                    const uncompletedTasks = [...that.data.uncompletedTasks];
                    uncompletedTasks.splice(index, 1);
                    that.setData({ uncompletedTasks }, that.updateProgress);
                } else {
                    const completedTasksList = [...that.data.completedTasksList];
                    completedTasksList.splice(index, 1);
                    that.setData({ completedTasksList }, that.updateProgress);
                }
            } else if (res.cancel) {
                // 用户点击取消
            }
        }
    });
},

});



